<?php

class OrderProductsController extends AppController {

    var $name = 'OrderProducts';

    function index() {
        $this->OrderProduct->recursive = 0;
        $this->set('orderProducts', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid order product', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('orderProduct', $this->OrderProduct->read(null, $id));
    }

    function add($order_id) {
        if (!empty($this->data)) {
            $this->OrderProduct->create();
            if ($this->OrderProduct->save($this->data)) {
                $this->Session->setFlash(__('Η αποθηκεύση έγινε επιτυχώς.', true));
                $this->redirect(array('controller' => 'orders', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        $products = $this->OrderProduct->Product->find('list');
        $orders = $this->OrderProduct->Order->find('list');
        $this->set(compact('products', 'orders'));
        $this->set('order_id', $order_id);
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid order product', true));
            $this->redirect(array('controller' => 'orders', 'action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->OrderProduct->save($this->data)) {
                $this->Session->setFlash(__('Η αποθηκεύση έγινε επιτυχώς.', true));
                $this->redirect(array('controller' => 'orders', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('Η αποθηκεύση απέτυχε. Παρακαλώ ξαναπροσπαθήστε.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->OrderProduct->read(null, $id);
        }
        $products = $this->OrderProduct->Product->find('list');
        $orders = $this->OrderProduct->Order->find('list');
        $this->set(compact('products', 'orders'));
    }

    function delete($id = null, $orderId = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for order product', true));
            if ($orderId == null) {
                $this->redirect(array('controller' => 'orders', 'action' => 'index'));
            } else {
                $this->redirect(array('controller' => 'orders', 'action' => 'edit', $orderId));
            }
        }
        if ($this->OrderProduct->delete($id)) {
            $this->Session->setFlash(__('Η διαγραφή έγινε επιτυχώς.', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Η διαγραφή απέτυχε.', true));
        $this->redirect(array('action' => 'index'));
    }

}
